Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
element-resize-detector
Advanced tools
The element-resize-detector npm package is a utility for detecting changes in the size of HTML elements. It provides a way to listen for resize events on any DOM element, which can be useful for responsive design, dynamic content adjustments, and more.
Basic Resize Detection
This feature allows you to listen for resize events on a specific DOM element. When the element is resized, the provided callback function is executed.
const elementResizeDetectorMaker = require('element-resize-detector');
const erd = elementResizeDetectorMaker();
const element = document.getElementById('myElement');
erd.listenTo(element, function(element) {
console.log('Element resized:', element);
});
Unlisten to Resize Events
This feature allows you to stop listening to resize events on a specific DOM element. This can be useful for cleanup or when the resize detection is no longer needed.
const elementResizeDetectorMaker = require('element-resize-detector');
const erd = elementResizeDetectorMaker();
const element = document.getElementById('myElement');
function onResize(element) {
console.log('Element resized:', element);
}
erd.listenTo(element, onResize);
// Later, if you want to stop listening to resize events
erd.uninstall(element);
Batch Listening
This feature allows you to listen for resize events on multiple elements at once. The provided callback function is executed whenever any of the elements are resized.
const elementResizeDetectorMaker = require('element-resize-detector');
const erd = elementResizeDetectorMaker();
const elements = document.querySelectorAll('.resize-listen');
function onResize(element) {
console.log('Element resized:', element);
}
elements.forEach(element => erd.listenTo(element, onResize));
The resize-observer-polyfill package provides a polyfill for the ResizeObserver API, which is a native browser API for observing changes to the size of an element. It offers similar functionality to element-resize-detector but leverages the native API when available, providing a more standardized approach.
The react-resize-detector package is a React-specific utility for detecting element resize events. It provides a higher-order component and hooks for integrating resize detection into React applications, making it a more seamless choice for React developers compared to element-resize-detector.
The vue-resize package is a Vue.js directive for detecting element resize events. It is tailored for Vue.js applications, providing a more idiomatic way to handle resize detection in Vue components compared to element-resize-detector.
Optimized cross-browser resize listener for elements. Up to 37x faster than related approaches (read section 5 of the article).
npm install element-resize-detector
Include the script in the browser:
<script src="node_modules/element-resize-detector/dist/element-resize-detector.min.js"></script>
This will create a global function elementResizeDetectorMaker
, which is the maker function that makes an element resize detector instance.
You can also require
it like so:
var elementResizeDetectorMaker = require("element-resize-detector");
// With default options (will use the object-based approach).
var erd = elementResizeDetectorMaker();
// With the ultra fast scroll-based approach.
// This is the recommended strategy.
var erdUltraFast = elementResizeDetectorMaker({
strategy: "scroll" //<- For ultra performance.
});
There is also an callOnAdd
option, which determines if listeners should be called when they are getting added. Default is true. If true, the listener is guaranteed to be called when it has been added. If false, the listener will not be guarenteed to be called when it has been added (does not prevent it from being called).
Listens to the element for resize events and calls the listener function with the element as argument on resize events. Options passed to the function will override the instance options.
Example usage:
erd.listenTo(document.getElementById("test"), function(element) {
var width = element.offsetWidth;
var height = element.offsetHeight;
console.log("Size: " + width + "x" + height);
});
Removes the listener from the element.
Removes all listeners from the element, but does not completely remove the detector. Use this function if you may add listeners later and don't want the detector to have to initialize again.
Completely removes the detector and all listeners.
If you need to listen to elements inside another document (such as an iframe), you need to init that document with this function. Otherwise the library won't be able to detect when elements are attached to the document. So for an iframe, simpy invoke erd.initDocument(iframe.contentDocument);
when the iframe is mounted on the DOM for the first time. The document from which the element resize detector instance is created will be initialized automatically. Notice that a new document is created when an iframe loads its content. So for iframes, be sure you invoke this function for each onLoad
iframe event.
position: static
it will be changed to position: relative
. Any unintentional top/right/bottom/left/z-index
styles will therefore be applied and absolute positioned children will be positioned relative to the element.Big thanks to Evry sponsoring this project.
This library is using the two approaches (scroll and object) as first described at http://www.backalleycoder.com/2013/03/18/cross-browser-event-based-element-resize-detection/.
The scroll based approach implementation was based on Marc J's implementation https://github.com/marcj/css-element-queries/blob/master/src/ResizeSensor.js.
Please note that both approaches have been heavily reworked for better performance and robustness.
A release that includes 1.1.15 and 1.1.16 with 1.2.0.
initDocument(document)
which is needed when listening to detached elements in other documents, such as iframes.important!
to most style properties, to avoid CSS overriding. Disabled by default.flex: none
. See #64.callOnAdd
being true. Also now removing onAnimationStart
listener when uninstalling. See #49.options.idHandler.get
.dir=RTL
.uninstall
supports being called with elements that haven't been initialized. uninstall
simply ignores non-erd elements.uninstall
now also supports a collection of elements.uninstall
may be called directly after a listenTo
call.window.getComputedStyle
instead of relying on the method being available in the global scope. This enables this library to be used in simulated browser environments such as jsdom.FAQs
Resize event emitter for elements.
The npm package element-resize-detector receives a total of 897,903 weekly downloads. As such, element-resize-detector popularity was classified as popular.
We found that element-resize-detector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.